#!/bin/bash
#backupAllDbs.sh
time="$(date +%Y%m%d%k%M%S)"
month="$(date +%Y%m)"
day="$(date +%Y%m%d)"
if [ -d "/var/backup" ]; then
        #statements
        dir=/var/backup/db_bak/$day
else
        dir=/var/log/mysql/db_bak/$day
fi

logfile=/var/backup/db_bak/backup-$month.log
test ! -d "$dir" && mkdir -p $dir
test ! -f "$logfile" && touch $logfile
echo "====>> $time begin to backup ,dir : $dir" >> $logfile
dbs=`mysql -uroot -pbxn500 -e "show databases;"| grep -v Database|grep -v _schema`

for db in $dbs;do
        mysqldump -uroot -pbxn500 $db > /tmp/$db.$day.sql;
done
mv /tmp/*.sql $dir/
files=`ls $dir`
echo "$(date +%Y%m%d%k%M%S) backup end. files:  $files <<====" >> $logfile
